//
//  TrashViewController.m
//  NanNote
//
//  Created by 郑楠 on 15/5/22.
//  Copyright (c) 2015年 ___NANteam___. All rights reserved.
//

#import "TrashViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface TrashViewController ()

@property(nonatomic, retain)NSMutableArray *modelArray;

@property(nonatomic, retain)UITableView *trashTblView;

@property(nonatomic, retain)CoreDataManager *dataManager;

@end

@implementation TrashViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.trashTblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.trashTblView.dataSource = self;
    self.trashTblView.delegate = self;
    self.trashTblView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.trashTblView];
    
    [self dataFromNoteEntity];
}

- (void)dataFromNoteEntity {
    self.dataManager = [CoreDataManager defaultCoreDataManager];
    // 查询
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NoteEntity" inManagedObjectContext:self.dataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deleteFlg != 0"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastUpdTime"
                                                                   ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.dataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects.count == 0) {
        NSLog(@"dataFromNoteEntity 0条");
    } else {
        self.modelArray = [NSMutableArray arrayWithArray:fetchedObjects];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    NoteEntity *note = self.modelArray[indexPath.row];
    cell.textLabel.text = note.noteContent;
    cell.detailTextLabel.text = note.lastUpdTime;
    NSLog(@"%@", note.noteBgColor);
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    if ([note.noteBgColor integerValue] == myYellowColor){
        [cell setBackgroundColor:[UIColor yellowColor]];
    } else if ([note.noteBgColor integerValue] == myOrangeColor) {
        [cell setBackgroundColor:[UIColor orangeColor]];
    } else if ([note.noteBgColor integerValue] == myCyanColor) {
        [cell setBackgroundColor:[UIColor cyanColor]];
    } else if ([note.noteBgColor integerValue] == myGreenColor) {
        [cell setBackgroundColor:[UIColor greenColor]];
    } else if ([note.noteBgColor integerValue] == myGrayColor) {
        [cell setBackgroundColor:[UIColor grayColor]];
    } else if ([note.noteBgColor integerValue] == myWhiteColor) {
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
